package com.lucky.ebidding.controller;

import com.lucky.ebidding.dto.TbAndZbDTO;
import com.lucky.ebidding.dto.TbAndZbIdDTO;
import com.lucky.ebidding.dto.ZbInfoAndModify;
import com.lucky.ebidding.dto.ZbModifyDTO;
import com.lucky.ebidding.model.User;
import com.lucky.ebidding.model.ZbInfo;
import com.lucky.ebidding.model.ZbModify;
import com.lucky.ebidding.service.PublishService;
import com.lucky.ebidding.service.UserService;
import com.lucky.ebidding.service.ZbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:publishZb
 * Package:com.lucky.ebidding.controller
 * Description:
 *
 * @Date:2020/3/17 12:55
 * @Author:857251389@qq.com
 */
@Controller
public class PublishZbController {

    @Autowired
    private ZbService zbService;
    @Autowired
    private UserService userService;

    @Autowired
    private PublishService publishService;

    @GetMapping("/zb_publishJump")
    public String publishJump(HttpServletRequest request,
                              Model model){


        User user = (User) request.getSession().getAttribute("user");

        int flag = userService.shStatus(user);
        if(flag == 3){
            model.addAttribute("msg","还没进行资格审核请先上传审核文件");
            return "zb/zb_publish";
        }

        if(flag == 0){
            model.addAttribute("msg","资格审核中，请耐心等待");
            return "zb/zb_publish";
        }

        if(flag == 2){
            model.addAttribute("msg","资格审核不通过请，重新上传审核文件");
            return "zb/zb_publish";
        }

        return "zb/zb_publish";
    }

    @GetMapping("/zb_bam")
    public String bam(){

        return "zb/zb_bam";
    }

    @GetMapping("/myZb")
    public String myzb(){

        return "zb/myZb";
    }

    /**
     * 招标项目填写
     * @param zbInfo
     * @param request
     * @param model
     * @return
     */
    @PostMapping("/publish")
    public String publish(ZbInfo zbInfo,HttpServletRequest request,
                          Model model){

        User user = (User) request.getSession().getAttribute("user");
        if(user.getStatus()== 0){
            int flag = userService.shStatus(user);
            if(flag == 3){
                model.addAttribute("msg","还没进行资格审核请先上传审核文件");
                return "zb/zb_publish";
            }

            if(flag == 0){
                model.addAttribute("msg","资格审核中，请耐心等待");
                return "zb/zb_publish";
            }

            if(flag == 2){
                model.addAttribute("msg","资格审核不通过，请重新上传审核文件");
                return "zb/zb_publish";
            }

            zbService.insert(zbInfo);

            return "zb/myZb";
        }

        zbService.insert(zbInfo);

        return "admin/zbModifySh";

    }


    @GetMapping("/publish/{id}")
    public String show(@PathVariable(name = "id") Integer id,
                       Model model){

        ZbInfo zbInfo = zbService.selectById(id);
        model.addAttribute("zbInfo",zbInfo);
        return "zb/zb_publish";
    }

    @GetMapping("/publish_edit/{id}")
    public String publishEdit(@PathVariable(name = "id") Integer zbId,
                       Model model){

        model.addAttribute("id",zbId);
        int i = zbService.isSh(zbId);
        if(i==0){
            ZbInfo zbInfo = zbService.selectById(zbId);
            model.addAttribute("zbInfo",zbInfo);
            return "zb/zb_publish";
        }
        return "zb/publish_edit";
    }

    @PostMapping("/zb_modify")
    public String zbModify(ZbModify zbModify){

        zbService.insertZbModify(zbModify);

        return "zb/myZb";
    }


    @GetMapping("/zb_result/{publishId}")
    public String zbResult(@PathVariable(name = "publishId") Integer publishId,
                           Model model){
        TbAndZbDTO tbANdZb = publishService.selectZbInfoAndTbInfoByPublishId(publishId);

        model.addAttribute("tbANdZb",tbANdZb);
        return "zb/zb_result";
    }

    @GetMapping("/zb_result1/{zbId}")
    public String zbResult1(@PathVariable(name = "zbId") Integer zbId,
                           Model model){
        TbAndZbDTO tbANdZb = publishService.selectZbInfoAndTbInfoByzbId(zbId);

        model.addAttribute("tbANdZb",tbANdZb);
        return "zb/zb_result";
    }

    @GetMapping("/zbModifySh")
    public String zbModifyShJump(){

        return "admin/zbModifySh";
    }


    @ResponseBody
    @GetMapping("/getZbModify")
    public ZbModifyDTO getZbModify(){

        List<ZbInfoAndModify> zbInfoAndModifyList =  zbService.selectZbInfoAndZbModify();

        ZbModifyDTO zbModifyDTO = new ZbModifyDTO();
        zbModifyDTO.setCode(0);
        zbModifyDTO.setCount(zbInfoAndModifyList.size());
        zbModifyDTO.setData(zbInfoAndModifyList);
        zbModifyDTO.setMsg("yes");

        return zbModifyDTO;
    }

    @GetMapping("/showModify/{zbId}")
    public String showModify(@PathVariable(name = "zbId") Integer zbId,Model model){
        List<ZbInfoAndModify> zbInfoAndModifyList = zbService.selectZbInfoAndZbModifyByzbId(zbId);

        model.addAttribute("zbInfoAndModify",zbInfoAndModifyList.get(0));
        return "zb/zb_modify";
    }
}
